package com.whfc.emp.service.impl;

import com.whfc.emp.dao.PlanCategoryMapper;
import com.whfc.emp.entity.PlanCategory;
import com.whfc.emp.service.FileUploadInfoService;
import com.whfc.emp.service.PlanCategoryService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@DubboService(interfaceClass = PlanCategoryService.class, version = "1.0.0",timeout = 30000)
public class PlanCategoryServiceImpl implements PlanCategoryService {

    @Resource
    private PlanCategoryMapper planCategoryMapper;

    @Override
    public boolean add(PlanCategory planCategory) {
        if (planCategory == null) {
            throw new IllegalArgumentException("参数不能为空");
        }
        if (ObjectUtils.isEmpty(planCategory.getPlanId())) {
            throw new IllegalArgumentException("工作计划不能为空");
        }
        if (ObjectUtils.isEmpty(planCategory.getCategoryName())) {
            throw new IllegalArgumentException("工种名称不能为空");
        }
        if (ObjectUtils.isEmpty(planCategory.getCreateUser())) {
            throw new IllegalArgumentException("创建人不能为空");
        }
        // 执行插入
        int rows = planCategoryMapper.insert(planCategory);
        return rows > 0;
    }

    @Override
    public List<PlanCategory> getByCategoryName(String categoryName) {
        // 参数校验
        if (ObjectUtils.isEmpty(categoryName)) {
            throw new IllegalArgumentException("工种名称不能为空");
        }
        // 执行查询
        return planCategoryMapper.selectByCategoryName(categoryName);
    }

    @Override
    public PlanCategory getById(Integer id) {
        // 参数校验
        if (ObjectUtils.isEmpty(id)) {
            throw new IllegalArgumentException("主键不能为空");
        }
        // 执行查询
        return planCategoryMapper.selectById(id);
    }

    @Override
    public boolean edit(PlanCategory planCategory) {
        // 参数校验
        if (planCategory == null) {
            throw new IllegalArgumentException("参数不能为空");
        }
        if (ObjectUtils.isEmpty(planCategory.getId())) {
            throw new IllegalArgumentException("主键不能为空");
        }
        // 执行修改（仅修改非空字段）
        int rows = planCategoryMapper.updateById(planCategory);
        return rows > 0;
    }

    @Override
    public boolean remove(Integer id) {
        // 参数校验
        if (ObjectUtils.isEmpty(id)) {
            throw new IllegalArgumentException("主键不能为空");
        }
        // 执行删除
        int rows = planCategoryMapper.deleteById(id);
        return rows > 0;
    }

    @Override
    public PlanCategory getPlanCategory(Integer planId, Integer userId) {
        PlanCategory py = planCategoryMapper.getPlanCategory(planId,userId);
        return py;
    }
}